package com.starblink.android.basic.widget

import android.content.Context
import android.graphics.Color
import android.util.AttributeSet
import android.util.TypedValue
import android.view.LayoutInflater
import android.widget.FrameLayout
import android.widget.ImageView.ScaleType
import com.starblink.android.basic.databinding.ViewCircleLogoBinding
import com.starblink.android.basic.ext.color
import com.starblink.android.basic.ext.dp
import com.starblink.android.basic.ext.visibleOrGone
import com.starblink.android.basic.extension.dp2px
import com.starblink.android.basic.util.ImageUtils
import com.starblink.basic.ext.load
import com.starblink.basic.style.R
import com.starblink.basic.util.CommUtils

/**
 * 商品、店铺 logo 视图（兼容无url时展示名称首字母）
 */
class CircleLogoView @JvmOverloads constructor(
    context: Context,
    attrSet: AttributeSet?,
    defStyleAttr: Int = 0,
) : FrameLayout(context, attrSet, defStyleAttr) {

    private val binding: ViewCircleLogoBinding
    private val avatar = R.mipmap.default_white

    var corner: Float = 10000f
        set(value) {
            field = value
            setRadius(value)
        }

    var boardWidth: Int = 0
        set(value) {
            field = value
            binding.cont.setPadding(
                value,
                value,
                value,
                value
            )
        }

    init {
        binding = ViewCircleLogoBinding.inflate(LayoutInflater.from(context), this, true)
        val typedArray = context.obtainStyledAttributes(
            attrSet,
            R.styleable.CircleLogoView
        )
        val webTextSize =
            typedArray.getDimensionPixelSize(R.styleable.CircleLogoView_webTextSize, 14).toFloat()
        binding.tvStore.setTextSize(TypedValue.COMPLEX_UNIT_PX, webTextSize)

        if (typedArray.hasValue(R.styleable.CircleLogoView_webBackground)) {
            val mBackgroundDrawable =
                typedArray.getDrawable(R.styleable.CircleLogoView_webBackground)
            binding.llStore.background = mBackgroundDrawable
        }

        if (typedArray.hasValue(R.styleable.CircleLogoView_webTextColor)) {
            val color =
                typedArray.getColor(
                    R.styleable.CircleLogoView_webTextColor,
                    Color.WHITE
                )
            binding.tvStore.setTextColor(color)
        }

        corner =
            typedArray.getDimensionPixelSize(R.styleable.CircleLogoView_logo_borderRadius, 10000)
                .toFloat()

        if (typedArray.hasValue(R.styleable.CircleLogoView_logo_bg)) {
            val logoBg = typedArray.getDrawable(R.styleable.CircleLogoView_logo_bg)
            binding.root.background = logoBg
        }

        if (typedArray.hasValue(R.styleable.CircleLogoView_iv_default_bg)) {
            val logoBg = typedArray.getDrawable(R.styleable.CircleLogoView_iv_default_bg)
            binding.ivStore.background = logoBg
        }

        val border =
            typedArray.getDimensionPixelSize(R.styleable.CircleLogoView_logo_border_width, 1)
        boardWidth = border.dp2px()

        val isShowBorder = typedArray.getBoolean(R.styleable.CircleLogoView_clv_isShowBorder, true)
        if (!isShowBorder) {
            boardWidth = 0
        }
        typedArray.recycle()

        //三星手机 Android6.0和7.0（可能不限于） 图片加载不出来
//        binding.root.enableCorner(10000)
    }

    private fun setRadius(radiu: Float) {
        val isCircle = radiu == 10000f
        if (!isCircle) {
            //binding.ivStore.setmRoundRadius(radiu.toInt())
            binding.layImg.setCornerRadius(radiu)
            binding.cont.setCornerRadius(radiu + 1.dp)
            binding.llStore.setCornerRadius(radiu)
        } else {
            //binding.ivStore.setmRoundRadius(radiu.toInt())
            binding.layImg.setCornerRadius(radiu)
            binding.cont.setCornerRadius(radiu)
            binding.llStore.setCornerRadius(radiu)
        }
    }

    fun load(
        url: String?,
        website: String?,
        scaleType: ScaleType = ScaleType.CENTER_INSIDE,
        hasDefaultHolder: Boolean = true,
        hasBgColor: Boolean = false,
    ) {
        binding.ivStore.scaleType = scaleType
        binding.llStore.visibleOrGone(false) {}
        binding.ivStore.visibleOrGone(true) {}
        if (url?.isNotEmpty() == true) {
            if (hasBgColor) {//增加图片背景
                ImageUtils.loadImageFilterTransSkGray100(
                    url,
                    binding.ivStore,
                    null, color(com.starblink.basic.style.R.color.white)
                )
            } else {
                binding.ivStore.load(url,
                    placeholder = if (hasDefaultHolder) CommUtils.getDrawable(avatar) else null,
                    errorCall = {
                        binding.tvStore.text = website?.firstOrNull()?.toString()
                        binding.llStore.visibleOrGone(true) {}
                        binding.ivStore.visibleOrGone(false) {}
                    })
            }
        } else {
            if (website?.isNotEmpty() == true) {
                binding.tvStore.text = website.firstOrNull()?.toString()?.uppercase()
                binding.llStore.visibleOrGone(true) {}
                binding.ivStore.visibleOrGone(false) {}
            } else {
                binding.ivStore.setImageResource(avatar)
            }
        }
    }

    fun setImageResource(resId: Int) {
        binding.llStore.visibleOrGone(false) {}
        binding.ivStore.visibleOrGone(true) {}
        binding.ivStore.setImageResource(resId)
    }
}